package com.powernode.money.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.powernode.money.mapper.FinanceAccountMapper;
import com.powernode.money.mapper.RechargeRecordMapper;
import com.powernode.money.model.FinanceAccount;
import com.powernode.money.model.RechargeRecord;
import com.powernode.money.service.RechargeRecordService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
@Component
@Service(interfaceClass = RechargeRecordService.class , version = "1.0.0" ,timeout = 15000)
public class RechargeRecordServiceImpl implements RechargeRecordService {

    @Autowired
    private RechargeRecordMapper rechargeRecordMapper;

    @Autowired
    private FinanceAccountMapper financeAccountMapper;


    @Override
    public List<RechargeRecord> queryByUid(Integer uid) {
        return rechargeRecordMapper.selectByUid(uid);
    }

    @Override
    public int add(int uid, String rechargeNo, double rechargeMoney, String rechargeDesc) {
        RechargeRecord record = new RechargeRecord();
        record.setRechargeDesc(rechargeDesc);
        record.setRechargeMoney(rechargeMoney);
        record.setRechargeNo(rechargeNo);
        record.setRechargeStatus("0");      //充值中
        record.setRechargeTime(new Date());
        record.setUid(uid);
        return rechargeRecordMapper.insert(record);
    }

    @Override
    public void updateRechargeSuccess(String rechargeNo) {
//查询充值记录
        RechargeRecord rechargeRecord = rechargeRecordMapper.selectByRechargeNo(rechargeNo);
        rechargeRecord.setRechargeStatus("1");
        //更新充值状态
        rechargeRecordMapper.updateByPrimaryKey(rechargeRecord);
        //查询需要更新的账号
        FinanceAccount financeAccount = financeAccountMapper.selectByUid(rechargeRecord.getUid());
        //更新用户的账号余额
        financeAccount.setAvailableMoney(financeAccount.getAvailableMoney()+rechargeRecord.getRechargeMoney());
        financeAccountMapper.updateByPrimaryKey(financeAccount);
    }

    @Override
    public void generateRecordPlan() {

        List<RechargeRecord> rechargeRecordList = rechargeRecordMapper.selectByRechargeStatus("0");

        for (RechargeRecord rechargeRecord : rechargeRecordList) {



        }
    }
}
